/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.hnjz.sys.user;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hnjz.common.AppException;
import com.hnjz.common.FyWebResult;
import com.hnjz.common.JsonResultUtil;
import com.hnjz.common.YnEnum;
import com.hnjz.common.security.CtxUtil;
import com.hnjz.common.util.LogUtil;
import com.hnjz.sys.role.Role;

/**
 * 用户管理的Controller
 * 
 * @author wumi
 * @version $Id: UserController.java, v 0.1 Jan 17, 2013 9:27:01 AM wumi Exp $
 */
@Controller
public class UserController {

    /**日志*/
    private static final Log log = LogFactory.getLog(UserController.class);

    @Autowired
    private UserManager      userManager;

    /**
     * 到用户管理的初始界面
     * 
     * @param model {@link ModelMap}
     * @return 用户管理的初始界面
     */
    @RequestMapping(value = "/userList.htm")
    public String userList(ModelMap model) {
        return "sys/user/userList";
    }

    /**
     * 用户管理的查询
     * 
     * @param model {@link ModelMap}
     * @param name 搜索条件，可以按名称，备注搜索
     * @param page 第几页
     * @param pageSize 每页显示多少条
     */
    @RequestMapping(value = "/userQuery.json", produces = "application/json")
    public void userQuery(ModelMap model, String name, String page, String pageSize) {
        page = StringUtils.defaultIfBlank(page, "1");
        FyWebResult re = userManager.queryUser(name, page, pageSize);
        JsonResultUtil.fyWeb(model, re);
    }

    /**
     * 删除一个用户
     * 
     * @param model  {@link ModelMap}
     * @param id 用户id
     */
    @RequestMapping(value = "/delUser.json", produces = "application/json")
    public void delUser(ModelMap model, String id) {
        try {
            userManager.removeUser(id);
            JsonResultUtil.suncess(model, "删除成功！");
        } catch (Exception e) {
            log.error("删除用户信息错误：", e);
            JsonResultUtil.fail(model, "删除失败！");
        }
    }

    /**
     * 保存一个用户信息
     * 
     * @param model {@link ModelMap}
     * @param funForm  用户管理的form表单
     * @return 用户管理的初始界面
     */
    @RequestMapping(value = "/saveUser.json", produces = "application/json")
    public void saveUser(ModelMap model, UserForm frm) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("表单信息:" + LogUtil.m(frm));
            }
            userManager.saveUser(frm);
            JsonResultUtil.suncess(model, "保存成功！");
        } catch (AppException e) {
            log.error("保存信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        }
    }

    /**
     * 编辑一个用户信息
     * 
     * @param id 用户Id
     * @return 用户管理的初始界面
     */
    @RequestMapping(value = "/editUser.htm")
    public String editUser(ModelMap model, UserForm frm) {
        if (log.isDebugEnabled()) {
            frm.setIsActive(YnEnum.Y.getCode());
            log.debug("id:" + frm.getId());
        }
        if (StringUtils.isBlank(frm.getId())) {
            return "sys/user/editUser";
        }
        User po = this.userManager.getUser(frm.getId());
        if(null != po){
            Role r = this.userManager.findRoleByUserId(frm.getId());
            frm.setJiece(r.getId());
        }
        frm.setId(po.getId());
        frm.setIssys(po.getIsSys());
        frm.setName(po.getName());
        frm.setUsername(po.getUsername());
        frm.setShouji2(po.getShouji2());
        frm.setShoujihao(po.getShoujihao());
        frm.setOrderby(String.valueOf(po.getOrderBy()));
        frm.setIsActive(po.getIsActive());
        return "sys/user/editUser";
    }

    /**
     * 修改密码
     * @param id 用户Id
     */
    @RequestMapping(value = "/editPas.htm")
    public String editPas(ModelMap model, UserForm frm) {
        model.addAttribute("id", CtxUtil.getCurUser().getId());
        return "sys/user/editPas";
    }

    /**
     * 保存密码
     * @param id 用户Id
     */
    @RequestMapping(value = "/savePas.json", produces = "application/json")
    public void savePas(ModelMap model, UserForm frm) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("表单信息:" + LogUtil.m(frm));
            }
            userManager.savePas(frm);
            JsonResultUtil.suncess(model, "更改密码成功");
        } catch (AppException e) {
            log.error("保存信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        }
    }

    /**
     * 保存密码
     * @param id 用户Id
     */
    @RequestMapping(value = "/resetPas.json", produces = "application/json")
    public void resetPas(ModelMap model, String id) {
        try {
            userManager.resetPas(id);
            JsonResultUtil.suncess(model, "重置密码成功");
        } catch (AppException e) {
            log.error("保存信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        }
    }

}
